package vip.idiy.anwei_android_iot.utils.dbHandle;


import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import vip.idiy.anwei_android_iot.domin.DeviceScene;
import vip.idiy.anwei_android_iot.utils.DatabaseUtil;

/**
 * ClassName SceneDataHandle
 * describe: 位置数据库处理
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 10:36 2023/4/20
 */
public class SceneDataHandle {
    private final String TAG = "myTag " + " SceneDataHandle ";

    private DatabaseUtil databaseUtil;

    private final String DATA_TABLE = "device_scene";
    // 每页返回记录数
    private final int LIMIT = 10;

    private final String COLUMNS_SCENE_ID = "scene_id";
    private final String COLUMNS_SCENE_NAME = "scene_name";
    private final String COLUMNS_SCENE_ICON = "scene_icon";

    private final String SELECTION_SCENE_NAME = "scene_name = ? ";
    private final String SELECTION_SCENE_ID = "scene_id = ? ";

    public SceneDataHandle(DatabaseUtil databaseUtil) {
        this.databaseUtil = databaseUtil;
    }

    /**
     * 添加
     *
     * @param deviceScene
     * @return
     */
    public boolean insert(DeviceScene deviceScene) {
        if (StringUtils.isEmpty(deviceScene.getSceneName()) || StringUtils.isEmpty(deviceScene.getSceneIcon()))
            return false;
        ContentValues values = new ContentValues();
        values.put(COLUMNS_SCENE_NAME, deviceScene.getSceneName());
        values.put(COLUMNS_SCENE_ICON, deviceScene.getSceneIcon());
        return databaseUtil.insert(DATA_TABLE, values) > 0;
    }

    /**
     * 根据sceneId更新数据
     *
     * @param deviceScene
     * @return
     */
    public boolean update(DeviceScene deviceScene) {
        ContentValues values = new ContentValues();
        values.put(COLUMNS_SCENE_NAME, deviceScene.getSceneName());
        values.put(COLUMNS_SCENE_ICON, deviceScene.getSceneIcon());
        return databaseUtil.update(DATA_TABLE, values, SELECTION_SCENE_ID, new String[]{String.valueOf(deviceScene.getSceneId())}) > 0;
    }

    /**
     * 根据sceneId删除数据
     *
     * @param deviceScene
     * @return
     */
    public boolean delete(DeviceScene deviceScene) {
        return databaseUtil.delete(DATA_TABLE, SELECTION_SCENE_ID, new String[]{String.valueOf(deviceScene.getSceneId())}) > 0;
    }

    /**
     * 根据位置名称获取记录
     *
     * @param sceneName
     * @return
     */
    public DeviceScene selectBySceneName(String sceneName) {
        Cursor cursor = databaseUtil.query(DATA_TABLE,
                new String[]{COLUMNS_SCENE_ID, COLUMNS_SCENE_NAME, COLUMNS_SCENE_ICON},
                SELECTION_SCENE_NAME,
                new String[]{sceneName}, null, null, null);
        DeviceScene deviceScene = new DeviceScene();
        if (cursor != null && cursor.moveToFirst()) {
            int idIndex = cursor.getColumnIndex(COLUMNS_SCENE_ID);
            if (idIndex >= 0) {
                deviceScene.setSceneId(cursor.getInt(idIndex));
            }
            int nameIndex = cursor.getColumnIndex(COLUMNS_SCENE_NAME);
            if (nameIndex >= 0) {
                deviceScene.setSceneName(cursor.getString(nameIndex));
            }
            int iconIndex = cursor.getColumnIndex(COLUMNS_SCENE_ICON);
            if (iconIndex >= 0) {
                deviceScene.setSceneIcon(cursor.getString(iconIndex));
            }
        }
        return deviceScene;
    }

    /**
     * 按位置名称查询记录是否存在
     *
     * @param sceneName
     * @return
     */
    public boolean isExist(String sceneName) {
        Cursor cursor = null;
        try {
            String[] columns = new String[]{COLUMNS_SCENE_NAME};
            String[] selectionArgs = new String[]{sceneName};
            cursor = databaseUtil.query(DATA_TABLE, columns, SELECTION_SCENE_NAME, selectionArgs, null, null, null);
            return cursor.getCount() > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return false;
    }

    /**
     * 获取列表
     *
     * @param columnNames
     * @param columnValues
     * @return
     */
    public List<DeviceScene> getList(String[] columnNames, String[] columnValues) {
        List<DeviceScene> dataList = new ArrayList<>();
        Cursor cursor = null;
        try {
            // 拼接查询参数
            StringBuilder selectionBuilder = new StringBuilder();
            if (columnNames == null || columnValues == null || columnNames.length == 0 || columnValues.length == 0) {
                cursor = databaseUtil.query(DATA_TABLE, null, null, null, null, null, null);

            } else {
                for (int i = 0; i < columnNames.length; i++) {
                    selectionBuilder.append(columnNames[i]).append("=?");
                    if (i != columnNames.length - 1) {
                        selectionBuilder.append(" AND ");
                    }
                }
                String selection = selectionBuilder.toString();
                cursor = databaseUtil.query(DATA_TABLE, null, selection, columnValues, null, null, null);
            }
            while (cursor.moveToNext()) {
                DeviceScene data = new DeviceScene();
                int idIndex = cursor.getColumnIndex(COLUMNS_SCENE_ID);
                if(idIndex>=0){
                    data.setSceneId(cursor.getInt(idIndex));
                }
                int nameIndex = cursor.getColumnIndex(COLUMNS_SCENE_NAME);
                if (nameIndex >= 0) {
                    data.setSceneName(cursor.getString(nameIndex));
                }
                int iconIndex = cursor.getColumnIndex(COLUMNS_SCENE_ICON);
                if (iconIndex >= 0) {
                    data.setSceneIcon(cursor.getString(iconIndex));
                }
                dataList.add(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return dataList;
    }

    /**
     * 获取列表
     *
     * @param columnNames
     * @param columnValues
     * @return
     */
    public List<DeviceScene> getList(String[] columnNames, String[] columnValues, int pageNum) {
        List<DeviceScene> dataList = new ArrayList<>();
        Cursor cursor = null;
        try {
            // 计算起始记录的偏移量
            int offset = (pageNum - 1) * LIMIT;
            // 拼接查询参数
            StringBuilder selectionBuilder = new StringBuilder();
            if (columnNames == null || columnValues == null || columnNames.length == 0 || columnValues.length == 0) {
                cursor = databaseUtil.query(DATA_TABLE, null, null, null, null, null, null, offset + "," + LIMIT);

            } else {
                for (int i = 0; i < columnNames.length; i++) {
                    selectionBuilder.append(columnNames[i]).append("=?");
                    if (i != columnNames.length - 1) {
                        selectionBuilder.append(" AND ");
                    }
                }
                String selection = selectionBuilder.toString();
                cursor = databaseUtil.query(DATA_TABLE, null, selection, columnValues, null, null, null, offset + "," + LIMIT);
            }
            while (cursor.moveToNext()) {
                DeviceScene data = new DeviceScene();
                int nameIndex = cursor.getColumnIndex(COLUMNS_SCENE_NAME);
                if (nameIndex >= 0) {
                    data.setSceneName(cursor.getString(nameIndex));
                }
                int iconIndex = cursor.getColumnIndex(COLUMNS_SCENE_ICON);
                if (iconIndex >= 0) {
                    data.setSceneIcon(cursor.getString(iconIndex));
                }
                dataList.add(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return dataList;
    }

    /**
     * 获取总记录数
     *
     * @return
     */
    public int getTotalRecordCount(String[] columnValues) {
        int count = 0;
        Cursor cursor = null;
        try {
            if (columnValues == null) {
                cursor = databaseUtil.rawQuery("SELECT COUNT(*) FROM " + DATA_TABLE, null);
            } else {
                cursor = databaseUtil.rawQuery("SELECT COUNT(*) FROM " + DATA_TABLE + " WHERE scene_name = ?", columnValues);
            }

            if (cursor.moveToFirst()) {
                count = cursor.getInt(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return count;
    }
}
